package cn.cxj.org.controller;

import cn.cxj.basic.util.AjaxResult;
import cn.cxj.basic.util.LogAnnotation;
import cn.cxj.basic.util.PageList;
import cn.cxj.org.domain.Employee;
import cn.cxj.org.query.EmployeeQuery;
import cn.cxj.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Value;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 员工接口
 * @author 陈鑫杰
 * 2022年1月20日10:11:38
 */
@RestController
@RequestMapping("/employee")
//@CrossOrigin 解决跨域传输 springmvc提供的注解
@Api(tags = "员工接口",description = "员工接口详细描述")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    /**
     * 新增或者修改 POST
     * @param employee 员工数据
     * @return
     */
    @PostMapping("/save")
    @ApiOperation(value = "员工添加或修改",notes = "如果有id就是修改否则就是添加")
    @LogAnnotation(operateType = "新增或者修改")
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        try {
            if (employee.getId() == null) {
                employeeService.add(employee);
            } else {
                employeeService.update(employee);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,稍后重试");
        }
    }

    /**
     * 删除 DELETE
     * @param id 员工id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "员工删除",notes = "删除时需要携带的id")
    public AjaxResult removeById(@PathVariable("id") Long id){
        try {
            employeeService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,稍后重试");
        }
    }

    /**
     * 基于id查询 GET
     * @param id 员工id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "基于id查询",notes = "用于查询的id")
    @LogAnnotation(operateType = "基于id查询")
    public Employee findById(@PathVariable("id") Long id){
        return employeeService.queryById(id);
    }

    /**
     * 获取所有 GET
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询全部")
    @LogAnnotation(operateType = "查询全部")
    public List<Employee> findAll(){
        return employeeService.queryAll();
    }


    /**
     * 分页和高级查询 POST
     * @param query 查询参数
     * @return
     */
    @PostMapping("/page")
    @LogAnnotation(operateType = "分页和高级查询")
    public PageList<Employee> pageQuery(@RequestBody EmployeeQuery query){
        return employeeService.queryPage(query);
    }

    /**
     * 批量删除
     * @param ids 员工id数组
     * @return
     */
    @PatchMapping
    @LogAnnotation(operateType = "批量删除")
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
            employeeService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,稍后重试");
        }
    }

    @GetMapping("/updateStateById")
    public AjaxResult updateStateById(Long id){

        try {
            employeeService.updateStateById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,稍后重试");
        }
    }
}
